import { ElMessageBox, ElMessage } from 'element-plus';
// import 'element-plus/es/components/message-box/style';
import { markRaw } from 'vue';
import { Delete } from '@element-plus/icons-vue';
import { ISchoolYearStage } from '../type';

export const useUtils = () => {
  return {
    openDeleteMsgBox: (callBack: Function, icon?: any) => {
      let config = {};
      if (icon)
        config = {
          confirmButtonText: '确 认',
          cancelButtonText: '取 消',
          type: 'error',
          draggable: true,
          icon: markRaw(icon),
        };
      else
        config = {
          confirmButtonText: '确 认',
          cancelButtonText: '取 消',
          type: 'error',
          draggable: true,
          icon: markRaw(Delete),
        };
      ElMessageBox.confirm('您将删除该学生信息,是否继续?', '提 示', config)
        .then(() => {
          callBack();
        })
        .catch(() => {
          ElMessage({
            type: 'success',
            message: '您取消了操作',
          });
        });
    },
    debounce: function (func: Function, wait = 2000, immediate = false) {
      let timeout: any;
      return () => {
        let context = this;
        let args = arguments;

        if (timeout) clearTimeout(timeout);
        if (immediate) {
          var callNow = !timeout;
          timeout = setTimeout(() => {
            timeout = null;
          }, wait);
          if (callNow) func.apply(context, args);
        } else {
          timeout = setTimeout(function () {
            func.apply(context, args);
          }, wait);
        }
      };
    },
    stampToTime: (timeStamp: string,str: string = '-') => {
      const date = new Date(Number(timeStamp));
      const Y = date.getFullYear() + str;
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + str;
      const D = date.getDate() + ' ';
      const h = date.getHours() + ':';
      const m = date.getMinutes() + ':';
      const s = date.getSeconds();
      return Y + M + D + h + m + s;
    },
    getSchoolYearList: () => {
      const minYear = 2000;
      const date = new Date();
      const maxYear = date.getFullYear();
      const schoolYearList = [] as { label:string;value:string }[];

      for (let index = maxYear; index > minYear; index--) {
        schoolYearList.push({ label:`${index - 1}-${index}学年`,value: `${index - 1}-${index}` })
      }
      return schoolYearList;
    },
    getGradeList: () => {
      const minYear = 2000;
      const date = new Date();
      const maxYear = date.getFullYear();
      const gradeList = [] as { label:string;value:string }[];

      for (let index = maxYear; index > minYear; index--) {
        gradeList.push({ label:`${index}级`,value: `${index}` })
      }
      return gradeList;
    },
    getNowSchoolYearStage: () => {
      const schoolYearStage = {} as ISchoolYearStage;
      const timeList = new Date().toLocaleDateString().split('/');  //  ['2023','1','14']  2022-2023的第一学期   ['2023','2','14']  2022-2023的第二学期  
      const year = Number(timeList[0]);
      const month = Number(timeList[1]);
      const day = Number(timeList[2]);
      if (month >= 2 && day >= 1 && month <= 8 && day <= 31) {
        schoolYearStage.schoolYear = `${year - 1}-${year}`;
        schoolYearStage.stage = '2'
      }
      if (month >= 9 && day >= 1) {
        schoolYearStage.schoolYear = `${year}-${year + 1}`;
        schoolYearStage.stage = '1'
      }
      if (month <= 1 && day <= 31) {
        schoolYearStage.schoolYear = `${year - 1}-${year}`;
        schoolYearStage.stage = '1'
      }

      return schoolYearStage;
    },
    getStuSchoolYear(grade: number) {
      const schoolYearList = []
      for (let index = grade; index < grade + 4; index++) {
        schoolYearList.unshift({label: `${index}-${index+1}学年`,value:`${index}-${index+1}`})
      }
      return schoolYearList;
    },
    buildUrlParams(paramsObj: any, url: string) {
      if(typeof paramsObj != 'object') {
        console.error('buildUrlParams方法：要转化为参数需以对象包裹');
        return '';
      }
      if(!url) {
        console.error('buildUrlParams方法：原url不能为空');
        return '';
      }
      const paramsList:string[] = [];
      Object.keys(paramsObj).forEach(key => {
        if(paramsObj[key]) paramsList.push(`${key}=${paramsObj[key]}`);
      })

      if(paramsList.length != 0) return `${url}?${paramsList.join('&')}`
      else return `${url}`
    }
  };
};
